import { ApiProperty, getSchemaPath } from "@nestjs/swagger";
import { CrudValidationGroups } from "@nestjsx/crud";
import { IsOptional, IsString, ValidateNested } from "class-validator";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { BaseEntity } from "./base.entity";
import { Group } from "./group.entity";
import { VillageActionType } from "./village-action-type.entity";
import { Village } from "./village.entity";
import { file } from "./about/file";

const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class VillageAction extends BaseEntity {

  @ApiProperty({
    description: '村组织活动-主键',
    required: false
  })
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => Village)
      }
    ]
  })
  @ManyToOne(() => Village, Village => Village.villageAction, {
    eager: true
  })
  village : Number | Village

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
  })
  @ManyToOne(
    () => Group,
    Group => Group.villageAction,
    {
      eager: true,
    },
  )
  group: Number | Group;
  

  @ApiProperty({
    example: 1,
    description: '村活动类型',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => VillageActionType)
      }
    ]
  })
  @ManyToOne(() => VillageActionType, VillageActionType => VillageActionType.villageAction)
  type: Number | VillageActionType

  @ApiProperty({
    example: '村组织活动名字',
    description: '村组织活动名字',
    required: true,
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  name: string

  @ApiProperty({
    example: '村组织活动 - 活动地点',
    description: '村组织活动 - 活动地点',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "char",
    length: 255,
    nullable: true
  })
  location: string

  @ApiProperty({
    example: '活动时间',
    description: '活动时间',
    required: true
  })
  @IsString({ always: true })
  @Column({
    type: "date",
  })
  time: Date

  @ApiProperty({
    example: '活动内容',
    description: '活动内容',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "text",
    nullable: true
  })
  content: string


  @ApiProperty({
    example: '相关文件',
    description: '相关文件',
    required: true
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  relatedDocuments: file []
}